//
//  MineChatViewController.swift
//  ModernFoodWebsite
//
//  Created by apple on 16/5/31.
//  Copyright © 2016年 Lonnie. All rights reserved.
//

import UIKit

struct Message {
    var fromMe = false
    var content:AnyObject = ""
    var showTime = true
    var time = NSDate()
}

class MineChatViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var tableView = UITableView(frame: CGRectZero, style: .Plain)
    let chatView = ChatBottomView(frame:CGRectZero)
    var messages:[Message] = [Message]()
    let dateFormatter = NSDateFormatter()
    var picker:ImagePicker?
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "lunar" 
        dateFormatter.dateFormat = "YYYY-MM-DD HH:mm:ss"
        addCommonStyleBackItem()
        view.addSubview(tableView)
        tableView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        tableView.registerClass(ChatImageCell.classForCoder(), forCellReuseIdentifier: "image")
        tableView.registerClass(ChatTextCell.classForCoder(), forCellReuseIdentifier: "text")
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100

        tableView.delegate = self
        tableView.dataSource = self

        tableView.separatorStyle = .None
        chatView.willSendTexthandler = {text in
            self.messages.append(Message(fromMe: true, content: text, showTime: true, time: NSDate()))
            self.tableView.reloadData()
            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.messages.count-1,inSection: 0), atScrollPosition: .Top, animated: true)
            self.friendWillSendMessage()
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4), dispatch_get_main_queue()
                , {
                 let message1 = Message(fromMe: false, content: ["我很好","你好么","ABC","哈哈哈",UIImage(named:"information_pic1")!,UIImage(named:"information_pic2")!,UIImage(named:"information_pic3")!,UIImage(named:"information_pic4")!][random()%8] , showTime: false, time: NSDate())
                 self.friendDidSendMessage(message1)
            })
        }
        chatView.willSendImageHandler = {
            self.picker = ImagePicker(context:self,complection:{ dic in
                let image = dic[UIImagePickerControllerOriginalImage] as! UIImage
                 self.messages.append(Message(fromMe: true, content: image, showTime: true, time: NSDate()))
                self.tableView.reloadData()
                self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.messages.count-1,inSection: 0), atScrollPosition: .Top, animated: true)
                self.friendWillSendMessage()
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4), dispatch_get_main_queue()
                    , {
                        let message1 = Message(fromMe: false, content: ["我很好","你好么","ABC","哈哈哈",UIImage(named:"information_pic1")!,UIImage(named:"information_pic2")!,UIImage(named:"information_pic3")!,UIImage(named:"information_pic4")!][random()%8], showTime: false, time: NSDate())
                        self.friendDidSendMessage(message1)
                })
            })
            self.picker!.pick()
        }
        chatView.showInView(self.view)
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "mine_more"), style: .Plain, action: { (item) in
            
        })
    }
    
    func friendWillSendMessage() {
    
    }
    func friendDidSendMessage(msg:Message) {
        self.messages.append(msg)
        self.tableView.reloadData()
        self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.messages.count-1,inSection: 0), atScrollPosition: .Top, animated: true)
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.messages.count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let message = self.messages[indexPath.row]
        if message.content is UIImage  {
            let cell = tableView.dequeueReusableCellWithIdentifier("image") as! ChatImageCell
            cell.fromMe = message.fromMe
            cell.showTime = message.showTime
            cell.headPotrait.image = UIImage(named: "information_pic1")
            cell.contentImageView.image = message.content as! UIImage
            
            cell.timeLabel.text = self.dateFormatter.stringFromDate(message.time)
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("text") as! ChatTextCell
            cell.fromMe = message.fromMe
            cell.showTime = message.showTime
            cell.headPotrait.image = UIImage(named: "information_pic1")
            cell.contentLabel.text = message.content as! String
            cell.timeLabel.text = self.dateFormatter.stringFromDate(message.time)
            return cell
        }
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50.0
    }
    
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}
